{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "编写Python将对全体数据进行标准化，并将数据p的特征进行重构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import warnings\n",
    "import pickle\n",
    "warnings.filterwarnings('ignore')\n",
    "\n",
    "tmp = pd.read_csv(\"data/p.csv\")\n",
    "t0_min = tmp.apply(lambda x: np.min(x), axis=0).values\n",
    "t0_ptp = tmp.apply(lambda x: np.ptp(x), axis=0).values\n",
    "t0 = tmp.apply(lambda x: (x - np.min(x)) / np.ptp(x), axis=0).values\n",
    "\n",
    "SEQLEN = 14\n",
    "dim_in = 108\n",
    "dim_out = 96\n",
    "pred_len = 30\n",
    "X_train = np.zeros((t0.shape[0]-SEQLEN-pred_len, SEQLEN, dim_in))\n",
    "Y_train = np.zeros((t0.shape[0]-SEQLEN-pred_len, dim_out),)\n",
    "X_test = np.zeros((pred_len, SEQLEN, dim_in))\n",
    "Y_test = np.zeros((pred_len, dim_out),)\n",
    "for i in range(SEQLEN, t0.shape[0]-pred_len):\n",
    "    Y_train[i-SEQLEN] = t0[i][0:96]\n",
    "    X_train[i-SEQLEN] = np.c_[t0[(i-SEQLEN):i],t0[i+1][96:].repeat(SEQLEN).reshape((6,SEQLEN)).T]\n",
    "for i in range(t0.shape[0]-pred_len, t0.shape[0]):\n",
    "    Y_test[i-t0.shape[0]+pred_len] = t0[i][0:96]\n",
    "    if i == t0.shape[0]-1:\n",
    "        # 这里weekday、trend、month和气温数据做了近似处理，正式使用时，需要使用天气预报的数据\n",
    "        X_test[i-t0.shape[0]+pred_len] = np.c_[t0[(i-SEQLEN):i],t0[i][96:].repeat(SEQLEN).reshape((6,SEQLEN)).T]\n",
    "    else:\n",
    "        X_test[i-t0.shape[0]+pred_len] = np.c_[t0[(i-SEQLEN):i],t0[i+1][96:].repeat(SEQLEN).reshape((6,SEQLEN)).T]\n",
    "\n",
    "with open('data/X_train.pkl', 'wb') as f:\n",
    "    pickle.dump(X_train, f)\n",
    "\n",
    "with open('data/Y_train.pkl', 'wb') as f:\n",
    "    pickle.dump(Y_train, f)\n",
    "\n",
    "with open('data/X_test.pkl', 'wb') as f:\n",
    "    pickle.dump(X_test, f)\n",
    "    \n",
    "with open('data/Y_test.pkl', 'wb') as f:\n",
    "    pickle.dump(Y_test, f)\n",
    "    \n",
    "with open('data/t0_ptp.pkl', 'wb') as f:\n",
    "    pickle.dump(t0_ptp, f)\n",
    "    \n",
    "with open('data/t0_min.pkl', 'wb') as f:\n",
    "    pickle.dump(t0_min, f)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
